import * as IConstant from './IConstant';
import Config from './Config';
import queryString from 'query-string';
import WebDB from './WebDB';
import { Modal } from 'antd-mobile';

export default class SysUtil
{
    // 检测是否登录
    static isUserLogined(store)
    {
        return store && store.me && store.me.userInfo && store.me.userInfo && store.me.userInfo.results && store.me.userInfo.results.ID;
    }

    // 获取用户当前位置
    static getLocation(store)
    {
        if(store && store.system && store.system.latitude && store.system.longitude)
        {
            return {
                lat: store.system.latitude,
                lng: store.system.longitude,
                address: store.system.address,
            };
        }
        let locationStr = WebDB.getItem(IConstant.STORAGE_KEY.USER_LOCATION);
        if(locationStr && locationStr.length > 0)
        {
            return JSON.parse(locationStr);
        }
        return {
            lat: Config.instance.appCityLatitude,
            lng: Config.instance.appCityLongitude,
        };
    }

    // 获取用户id
    static getUserId(store)
    {
        if(this.isUserLogined(store))
        {
            return store.me.userInfo.results.ID;
        }
        let userInfo = this._getUserInfoFromLocalStorage();
        return userInfo ? userInfo.ID : undefined;
    }

    // 获取用户信息
    static getUserInfo(store)
    {
        if(this.isUserLogined(store))
        {
            return store.me.userInfo.results;
        }
        return this._getUserInfoFromLocalStorage();
    }

    static _getUserInfoFromLocalStorage()
    {
        let str = WebDB.getItem(IConstant.STORAGE_KEY.USER_INFO);
        if(str && str.length)
        {
            return JSON.parse(str);
        }
    }

    // 例如传入：'nearBy.hotelList'，返回store['nearBy']['hotelList']
    static getStoreItemByDotConnector(store, dotConnector)
    {
        if(!store || !dotConnector)
        {
            return undefined;
        }
        let dotConnectors = dotConnector.split('.');
        let state = store;
        for(let i = 0; i < dotConnectors.length; i++)
        {
            let _state = state[dotConnectors[i]];
            if(!_state)
            {
                return _state;
            }
            state = _state;
        }
        return state;
    }

    //输出指定n位数的随机数的随机整数
    static RndNum(n) {
        let rnd = '';
        for (let i = 0; i < n; i++) {
            rnd += Math.floor(Math.random() * 10);
        }
        return rnd;
    }
    //身份证号校验
    static ValidateIdcard(id) {
        if (id && (/^[1-9]\d{5}(18|19|([2]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/i.test(id) || /^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$/i.test(id))) {
            return true;
        } else {
            return false;
        }
    }

    static isBrowser()
    {
        return typeof window !== 'undefined';
    }

    //获取当前Url中的参数
    static getUrlParameter(key) 
    {
        if(!this.isBrowser())
        {
            return null;
        }
        let urlSearch = queryString.parse(window.location.search);
        if(urlSearch)
        {
            return urlSearch[key];
        }
        return null;
    }

    // 是否是用微信浏览器打开
    static ifWechatBrowser()
    {
        // 对浏览器的UserAgent进行正则匹配，不含有微信独有标识的则为其他浏览器
        var useragent = navigator.userAgent;
        if (useragent.match(/MicroMessenger/i) !== 'MicroMessenger')
        {
            // // 这里警告框会阻塞当前页面继续加载
            // alert('已禁止本次访问：您必须使用微信内置浏览器访问本页面！');
            // // 以下代码是用javascript强行关闭当前页面
            // var opened = window.open('about:blank', '_self');
            // opened.opener = null;
            // opened.close();
            return false;
        }
        return true;
    }
    //获取当前时间 ， 格式为 yyyyMMddHHmmss
    static getCurrentDateTime()
    {
        let date = new Date();
        let maxCount = 10;
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let seconds = date.getSeconds();
        let dateString = '' + date.getFullYear() +
                        (month >= maxCount ? '' : (0)) +
                        month +
                        (day >= maxCount ? '' : (0)) +
                        day +
                        (hours >= maxCount ? '' : (0)) +
                        hours +
                        (minutes >= maxCount ? '' : (0)) +
                        minutes +
                        (seconds >= maxCount ? '' : (0)) +
                        seconds;
        return dateString;
    }
}